package com.example.demo2_review.thread_local_example;

import com.alibaba.fastjson.JSONObject;
import com.example.common.pojo.ThreadLocalData;
import com.example.common.utils.thread_local.CurrentThreadUtil;
import com.example.common.utils.thread_pool.ExecutorUtil;
import com.example.common.utils.thread_pool.TaskExecutorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.time.LocalDateTime;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author zwp14
 * @create 2024/6/21 15:26
 * @description ThreadLocal
 */
@Slf4j
public class ThreadLocalExample {

    public static void main(String[] args) {
        ThreadPoolTaskExecutor taskExecutor = TaskExecutorUtil.defaultTaskExecutor();
        taskExecutor.execute(() -> {
            long id = Thread.currentThread().getId();
            String name = Thread.currentThread().getName();
            CurrentThreadUtil.setThreadLocal(ThreadLocalData.builder()
                    .num(id)
                    .str(name).build());
            log.info("taskExecutor1:{}",JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
            System.out.println(JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
        });
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                long id = Thread.currentThread().getId();
                String name = Thread.currentThread().getName();
                CurrentThreadUtil.setThreadLocal(ThreadLocalData.builder()
                        .num(id)
                        .str(name).build());
                log.info("taskExecutor2:{}",JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
                System.out.println(JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
            }
        });


        ThreadPoolExecutor executor = ExecutorUtil.defaultExecutor();
        executor.execute(()->{
            long id = Thread.currentThread().getId();
            String name = Thread.currentThread().getName();
            CurrentThreadUtil.setThreadLocal(ThreadLocalData.builder()
                    .num(id)
                    .str(name).build());
            log.info("executor1:{}",JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
            System.out.println(JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                long id = Thread.currentThread().getId();
                String name = Thread.currentThread().getName();
                CurrentThreadUtil.setThreadLocal(ThreadLocalData.builder()
                        .num(id)
                        .str(name).build());
                log.info("executor2:{}",JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
                System.out.println(JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
            }
        });

        CurrentThreadUtil.setThreadLocal(ThreadLocalData.builder()
                .str(Thread.currentThread().getName())
                .num(Thread.currentThread().getId())
                .time(LocalDateTime.now()).build());
        log.info("main:{}",JSONObject.toJSONString(CurrentThreadUtil.getThreadLocal()));
        System.out.println(CurrentThreadUtil.getThreadLocal());

        executor.shutdown();
        taskExecutor.shutdown();

    }
}
